<?php
/**
 * 公告
 */
namespace App;
defined('IN_IA') or exit('Access Denied');

use Model\ReadNoticeMod;
use \Pub\tool\Setting as SettingCache;
class Notice extends Action
{
    private $ReadNotice = null;
    public function __construct()
    {
        parent::__construct();
        $this->getUid();
    }

    /**
     * 获取公告
     * @param int uid 用户id
     */
    public function getNotice()
    {
        $this->ReadNotice = new ReadNoticeMod();
        $_read_notice = $this->ReadNotice->findOne(array(
            "uniacid"=>$this->_uniacid,
            "uid"=>$this->_uid
        ),array("time"));
        $_notice = SettingCache::get(SettingCache::NOTICE);
        if(!empty($_notice) && (!$_read_notice || $_read_notice["time"] != SettingCache::get(SettingCache::NOTICE_TIME))){
            $this->_result = array("status"=>"success","data"=>$_notice);
        }
        $this->result($this->_result);
    }

    /**
     * 已读公告
     * @param int uid 用户id
     */
    public function read()
    {
        $this->ReadNotice = new ReadNoticeMod();
        $_read_notice = $this->ReadNotice->findOne(array(
            "uniacid"=>$this->_uniacid,
            "uid"=>$this->_uid
        ),array("id"));
        if(!$_read_notice){
            $_data = array("uniacid"=>$this->_uniacid,"uid"=>$this->_uid,"time"=>SettingCache::get(SettingCache::NOTICE_TIME));
            $this->ReadNotice->insert($_data);
        }else{
            $this->ReadNotice->update(array(
                "time"=>SettingCache::get(SettingCache::NOTICE_TIME)
            ),array("id"=>$_read_notice["id"]));
        }
    }
}